<template>
  <div>
    <el-row class="staffTeamBg">
      <el-col :span="4">
        <!-- 分组 -->
        <div class="mt-20">
          <el-tree ref="tree" :data="treeData1" :props="treeProps" :expand-on-click-node="false" default-expand-all highlight-current @node-click="handleNodeClick">
            <div class="custom-tree-node" slot-scope="{ node, data }">
              <el-tooltip class="item" effect="dark" :content="node.label" placement="bottom">
                <span style="width: 100px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;">{{ node.label }}</span>
              </el-tooltip>
              <el-dropdown trigger="click" v-if="data.groupId">
                <i class="el-icon-more" @click.stop></i>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item @click.native="editGroupFn(data)" style="color: #082aff">编辑</el-dropdown-item>
                  <el-dropdown-item @click.native="deleteGroupFn(node, data)" style="color: #082aff">删除</el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
              <span class="add" @click.stop="handleAddItem1" v-if="!data.groupId">
                <i class="el-icon-circle-plus-outline"></i>&nbsp;添加
              </span>
            </div>
          </el-tree>
        </div>
      </el-col>
      <el-col :span="20" style="border-left: 1px solid #eee; padding: 16px 15px 20px 15px;">
        <div style="border-bottom: 1px solid #eee;">
          <NormalForm pageTitle="员工组管理" :isShowCard="false" :operateForm="queryForm" :formList="formList" @specialSearch="handleQuery"></NormalForm>
        </div>
        <div>
          <div class="right-btn" style="margin-right: 0;">
            <el-button type="primary" @click="addGroupStaffFn" icon="el-icon-plus">添加员工
            </el-button>
          </div>
          <el-table v-loading="loading" :data="tableList" style="width: 100%" height="475">
            <el-table-column type="index" width="55" align="center" label="序号" />
            <el-table-column label="姓名" align="center" prop="name" show-overflow-tooltip>
              <template slot-scope="{ row }">
                <span style="color: #082aff">{{ row.name }}</span>
              </template>
            </el-table-column>
            <el-table-column label="手机" prop="mobile" align="center" show-overflow-tooltip></el-table-column>
            <el-table-column label="邮箱" align="center" prop="email" show-overflow-tooltip />
            <el-table-column label="企业微信ID" align="center" prop="userId" show-overflow-tooltip>
              <template slot-scope="{ row }">
                <span style="color: #082aff">{{ row.userId }}</span>
              </template>
            </el-table-column>
            <el-table-column label="在职状态" align="center" prop="isActivate" show-overflow-tooltip>
              <template slot-scope="scope">{{ statusActivate[scope.row.isActivate] }}</template>
            </el-table-column>
            <el-table-column label="岗位" align="center" prop="createTime" show-overflow-tooltip>
            </el-table-column>
            <el-table-column label="渠道" align="center" prop="department" show-overflow-tooltip>
              <template slot-scope="scope">{{ statusActivates[scope.row.department] }}</template>
            </el-table-column>
            <el-table-column label="操作" align="center" fixed="right" width="120" class-name="small-padding fixed-width">
              <template slot-scope="{ row }">
                <div style="display: flex; justify-content: space-around">
                  <el-button size="mini" type="text" @click="deleteItem(row)">移除</el-button>
                </div>
              </template>
            </el-table-column>
          </el-table>
          <NormalPagination :currentPage="queryForm.pageNum" :pageSize="queryForm.pageSize" :total="total" @handleSizeChange="handleSizeChange" @handleCurrentChange="handleCurrentChange"></NormalPagination>
        </div>
      </el-col>
    </el-row>
    <el-dialog :title="treeForm.id ? '编辑' : '添加' + '分组'" :visible.sync="treeDialogVisible" width="400px"
      :close-on-click-modal="false" :close-on-press-escape="false">
      <el-form ref="treeForm" :model="treeForm" label-width="120px">
        <el-form-item label="请输入分组名称">
          <el-input v-model="treeForm.name" maxlength="32" show-word-limit placeholder="请输入"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button type="info" plain @click="treeDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="treeSubmit">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 删除分组 -->
    <el-dialog :title="'删除' + (treeForm.id ? '失败' : '确认')" :visible.sync="delVisible" width="400px"
      :close-on-click-modal="false" :close-on-press-escape="false">
      <span>确认删除该分组？</span>
      <div slot="footer">
        <el-button type="info" plain @click="delVisible = false">取 消</el-button>
        <el-button type="primary" @click="treeDelSubmit">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 删除table数据 -->
    <el-dialog title="删除" :visible.sync="delTable" width="400px" :close-on-click-modal="false" :close-on-press-escape="false">
      <span>确认删除该条数据吗？</span>
      <div slot="footer">
        <el-button type="info" plain @click="cancelDel">取 消</el-button>
        <el-button type="primary" @click="deleteLink">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 新增员工弹窗 -->
    <SelectUser v-if="staffDialog" title="添加员工" :visible.sync="staffDialog" @confirm="selectUserConfirm" />
  </div>
</template>

<script>
import SelectUser from "@/components/SelectUser/index.vue";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { createdCascader, mechanismCascader } from "@/components/index.js";
import { addGroup, getGroup, editGroup, deleteGroup, getStaffList, addStaff, deleteStaff } from "@/api/system/staff.js";

export default {
  name: "Insurance",
  components: { Treeselect, createdCascader, mechanismCascader, SelectUser },
  data() {
    return {
      queryForm: {
        groupId: "",
        userName: "",
        pageNum: 1,
        pageSize: 20,
      },
      formList: [
        {
          label: '员工名称',
          value: '',
          prop: 'userName',
          type: 'INPUT',
          emunName: ''
        },
      ],
      loading: false,
      tableList: [],
      total: 0,
      treeDialogVisible: false,
      staffDialog: false,
      delVisible: false,
      delTable: false,
      riskId: "",
      treeData1: [
        {
          id: "",
          name: "全部分组",
          parentId: 0,
          hasParent: false,
          hasChildren: true,
          children: [],
        },
      ],
      treeProps: {
        children: "children",
        label: "name",
      },
      treeForm: {
        id: "",
        name: "",
      },
      delGroupId: "",
      statusActivate: { 1: "在职", 2: "在职", 4: "在职", 5: "不在职", 6: "不在职" },
      statusActivates: { 1: "玛卡巴卡", 2: "唔西迪西", 3: "权限测试" },
    };
  },
  created() {
    this.getCategoryList()
  },
  methods: {
    // 获取分组
    getCategoryList() {
      getGroup().then((res) => {
        if (res.code === 200) {
          const { rows } = res;
          this.treeData1[0].children = rows;
        }
      });
    },
    // 远程搜索任务名称 + 搜索
    handleQuery() {
      this.queryForm.pageNum = 1;
      this.getStaffListFn();
    },
    // 翻页
    handleSizeChange(val) {
      this.queryForm.pageNum = 1;
      this.queryForm.pageSize = val;
      this.getStaffListFn();
    },
    handleCurrentChange(val) {
      this.queryForm.pageNum = val;
      this.getStaffListFn();
    },
    getStaffListFn() {
      getStaffList(this.queryForm).then((res) => {
        this.tableList = res.rows || [];
        this.total = res.total || 0;
      });
    },
    // 选择员工
    selectUserConfirm(value) {
      let wxUserList = [];
      value.forEach((item) => {
        wxUserList.push({ userId: item.userId });
      });
      this.tableList.forEach((item) => {
        wxUserList.push({ userId: item.userId });
      });

      addStaff({ wxUserList, groupId: this.queryForm.groupId }).then((res) => {
        if (res.code === 200) {
          this.$message.success(res.data);
        }
        this.handleQuery();
      });
    },
    handleNodeClick(data) {
      if (data.groupIdValue){
        this.queryForm.groupId = data.groupIdValue;
        this.handleQuery();
      }
    },
    // 编辑分组
    editGroupFn(data) {
      this.treeForm.groupId = data.groupIdValue;
      this.treeForm.name = data.name;
      this.companyFlag = true;
      this.treeDialogVisible = true;
    },
    // 添加分组
    handleAddItem1() {
      this.treeDialogVisible = true;
    },
    // 变更分组
    treeSubmit() {
      // 编辑
      if (this.treeForm.groupId) {
        editGroup({ groupId: this.treeForm.groupId, name: this.treeForm.name }).then((res) => {
          if (res.code === 200) {
            console.log(res, "=====res");
            this.$message.success(res.data);
            this.treeDialogVisible = false;
            this.treeForm.name = "";
            this.treeForm.groupId = "";
            this.getCategoryList();
          } else {
            this.$message.error(res.msg);
          }
        });
      } else {
        // 新增
        addGroup({ name: this.treeForm.name }).then((res) => {
          if (res.code === 200) {
            this.$message.success(res.data);
            this.treeDialogVisible = false;
            this.treeForm.name = "";
            this.treeForm.groupId = "";
            this.getCategoryList();
          } else {
            this.$message.error(res.msg);
          }
        });
      }
    },
    // 新增员工
    addGroupStaffFn() {
      this.staffDialog = true;
    },
    // 删除分组
    deleteGroupFn(node, data) {
      this.delVisible = true;
      this.delGroupId = data.groupIdValue;
    },
    // 删除
    treeDelSubmit() {
      deleteGroup(this.delGroupId).then((res) => {
        this.delVisible = false;
        if (res.code === 200) {
          this.$message.success(res.data);
        } else {
          this.$message.error(res.msg);
        }
        this.getCategoryList();
      });
    },
    // 删除分组人员
    deleteItem(row) {
      this.delTable = true;
      this.riskId = row.userId;
    },
    // 确认删除
    deleteLink() {
      deleteStaff({ 'wxUserId': this.riskId, 'groupId': this.queryForm.groupId }).then((res) => {
        if (res.code === 200) {
          this.$message.success(res.data);
          this.cancelDel();
          this.handleQuery();
        }
      });
    },
    // 取消删除
    cancelDel() {
      this.delTable = false;
      this.riskId = "";
    },
  },
};
</script>

<style lang="scss" scoped>
@import "@/styles/base.scss";
::v-deep .el-table th.el-table__cell.is-leaf,
.el-table td.el-table__cell {
  border-bottom: none !important;
}

::v-deep .el-table .el-table__header-wrapper th {
  background-color: #f8f8f9 !important;
}
.custom-tree-node {
  width: 100%;
  display: flex;
  justify-content: space-between;
  padding-right: 10px;
  font-size: 12px;

  .label-info {
    width: 100px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}
.add {
  color: #082aff;
}
.staffTeamBg {
  background: #fff;

  ::v-deep {

    .el-tree {
      padding: 10px 10px 0 10px;
    }

    .el-tree-node__content {
      height: 35px;
      padding-left: 10px !important;
    }
  }
}
</style>
